package com.itheima.controller;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    // 注入UserMapper
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param pageNum 当前页数
     * @param pageSize 每页页数
     * @return 给浏览器返回的数据
     */
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "2")Integer pageSize,String name,Short age, Short gender,String phone){

        // 查询数据
        PageBean pageBean = userService.page(pageNum,pageSize,name,age,gender,phone);

        // 返回给浏览器
        return Result.success(pageBean);
        //最后一步，会不会用postman发请求
    }

    /**
     * 根据id获取数据
     * @param id id
     * @return 给浏览器返回数据
     */
    @GetMapping("/userById/{id}")
    public Result userById(@PathVariable Integer id){

        User user  = userService.userById(id);

        return Result.success(user);
    }

    /**
     * 根据id修改员工
     * @param user
     * @return
     */
    @PutMapping("updateById")
    public Result updateUser(@RequestBody User user){
        userService.updateUser(user);

        return Result.success();
    }


    /**
     * 批量删除
     * @param ids ids
     * @return 0
     */
    @DeleteMapping("/delete/{ids}")
    public Result deleteById(@PathVariable Integer[] ids){
        userService.deleteById(ids);
        return Result.success();
    }



}
